/**
 * Copyright (C), 2020-2021 by colley.ma
 * File Name: PageInfo.java
 * Encoding: UTF-8
 * Date: 2021年12月29日 上午11:27:36
 * History:
*/
package com.swak.frame.dto;

import com.swak.frame.dto.base.DTO;

public class PageInfo implements DTO {
	private static final long serialVersionUID = -8141622693773776209L;
	public static int DEFAULT_PAGE_SIZE = 10;
	protected int pageIndex;
	protected int pageSize = DEFAULT_PAGE_SIZE;
	protected int totalRows;

	public PageInfo(int pageIndex, int pageSize) {
		this(pageIndex);
		this.pageSize = pageSize;
	}

	public PageInfo(int pageIndex) {
		this.pageIndex = pageIndex;
	}

	public PageInfo() {
	}

	public static PageInfo page(int pageIndex) {
		return new PageInfo(pageIndex);
	}

	public static PageInfo page(int pageIndex, int pageSize) {
		return new PageInfo(pageIndex,pageSize);
	}
	
	public static PageInfo pageSize(int pageSize) {
		return new PageInfo(1,pageSize);
	}

	public static int getDefaultPageSize() {
		return DEFAULT_PAGE_SIZE;
	}

	public static void setDefaultPageSize(int defaultPageSize) {
		DEFAULT_PAGE_SIZE = defaultPageSize;
	}

	public int getPageIndex() {
		return pageIndex;
	}

	public void setPageIndex(int pageIndex) {
		this.pageIndex = pageIndex;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getTotalRows() {
		return totalRows;
	}

	public void setTotalRows(int totalRows) {
		this.totalRows = totalRows;
	}

	public int getOffset() {
		return pageIndex > 0 ? ((pageIndex - 1) * pageSize) : 0;
	}

	public int getTotalPages() {
		return new Integer((new Double(Math.ceil(new Double(totalRows) / new Double(pageSize)))).intValue());
	}
}
